/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package obiektyturystyczne;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**

 @author konrad
 */
public class ResultWindow extends javax.swing.JFrame {

	/**
	 Creates new form ResultWindow
	 */
	MainWindow main;
	int obiekt;
	Double lat, lng;

	public ResultWindow(int obiekt, MainWindow main) {
		this.main = main;
		this.obiekt = obiekt;

		initComponents();
		try {
			Statement statement = main.connection.createStatement();
			String query = "select * from obiekty o left join typy_obiektow t on (typ=t.id) where o.id=" + obiekt + ";";
			ResultSet rs = statement.executeQuery(query);
			rs.next();
			labelAdres.setText(rs.getString("adres"));
			labelNazwa.setText(rs.getString("nazwa"));
			labelTelefon.setText(rs.getString("telefon"));
			labelWWW.setText(rs.getString("strona_www"));
			labelTyp.setText(rs.getString("opis"));
			lat = rs.getDouble("szerokosc");
			lng = rs.getDouble("dlugosc");

			DefaultListModel atr = new DefaultListModel();
			jList1.setModel(atr);
			query = "select a.id,opis from obiekty o join obiekty_atrakcje on (o.id=obiekt) join atrakcje a on (a.id=atrakcja) where obiekt=" + obiekt + ";";
			rs = statement.executeQuery(query);
			while (rs.next()) {
				atr.addElement(new IndeksWartosc(rs.getInt("id"), rs.getString("opis")));
			}

			DefaultListModel wyp = new DefaultListModel();
			jList2.setModel(wyp);
			query = "select w.id,opis from obiekty o join obiekty_wyposazenie on (o.id=obiekt) join wyposazenie w on (w.id=wyposazenie) where obiekt=" + obiekt + ";";
			rs = statement.executeQuery(query);
			while (rs.next()) {
				wyp.addElement(new IndeksWartosc(rs.getInt("id"), rs.getString("opis")));
			}

			query = "select ile_miejsc,ilosc,cena from obiekty o join obiekty_pokoje on (o.id=obiekt) where obiekt=" + obiekt + ";";
			rs = statement.executeQuery(query);
			((DefaultTableModel) tablePokoje.getModel()).removeRow(0);
			while (rs.next()) {
				DefaultTableModel model = (DefaultTableModel) tablePokoje.getModel();
				model.insertRow(tablePokoje.getRowCount(), new Object[]{rs.getInt("ile_miejsc"), rs.getInt("ilosc"), rs.getInt("cena")});
			}

			query = "select data, komentarz from komentarze where obiekt=" + obiekt;
			rs = statement.executeQuery(query);
			
			while (rs.next()) {
				//listModel.addElement(rs.getString("data") + ": " + rs.getString("komentarz"));
                                textAreaKomentarze.append(rs.getString("data") + ": " + rs.getString("komentarz") + "\n\n");
			}


		} catch (SQLException ex) {
			Logger.getLogger(ResultWindow.class.getName()).log(Level.SEVERE, null, ex);
		}

	}

	/**
	 This method is called from within the constructor to initialize the form.
	 WARNING: Do NOT modify this code. The content of this method is always
	 regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        labelWWW = new javax.swing.JLabel();
        labelTelefon = new javax.swing.JLabel();
        labelAdres = new javax.swing.JLabel();
        labelNazwa = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        scrollPanePokoje = new javax.swing.JScrollPane();
        tablePokoje = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        labelTyp = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        labelKomentarze = new javax.swing.JLabel();
        labelPodzielOpinia = new javax.swing.JLabel();
        buttonDodajKomentarz = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        textFieldKomentarz = new javax.swing.JTextPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        textAreaKomentarze = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nazwa");

        jLabel2.setText("Adres");

        jLabel3.setText("Telefon");

        jLabel4.setText("Strona www");

        labelWWW.setText("jLabel5");

        labelTelefon.setText("jLabel6");

        labelAdres.setText("jLabel7");

        labelNazwa.setText("jLabel8");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel5.setText("Atrakcje");

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        jLabel6.setText("Wyposażenie");

        tablePokoje.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                { new Integer(0),  new Integer(0),  new Integer(0)}
            },
            new String [] {
                "Liczba pokoi", "Ilość miejsc", "Cena"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tablePokoje.setColumnSelectionAllowed(true);
        tablePokoje.setMinimumSize(new java.awt.Dimension(0, 0));
        scrollPanePokoje.setViewportView(tablePokoje);

        jLabel7.setText("Pokoje");

        jLabel8.setText("Typ");

        labelTyp.setText("jLabel9");

        jButton1.setText("Edytuj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        labelKomentarze.setText("Komentarze:");

        labelPodzielOpinia.setText("Podziel się opinią!");

        buttonDodajKomentarz.setText("Dodaj komentarz");
        buttonDodajKomentarz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDodajKomentarzActionPerformed(evt);
            }
        });

        jScrollPane4.setViewportView(textFieldKomentarz);

        textAreaKomentarze.setColumns(20);
        textAreaKomentarze.setLineWrap(true);
        textAreaKomentarze.setRows(5);
        jScrollPane5.setViewportView(textAreaKomentarze);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelTyp)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(labelAdres, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelTelefon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelWWW, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelNazwa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6)))
                            .addComponent(labelKomentarze)
                            .addComponent(jScrollPane5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(scrollPanePokoje, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(labelPodzielOpinia))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jScrollPane4)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(buttonDodajKomentarz)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labelNazwa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelAdres))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(labelTelefon))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(labelWWW))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(labelTyp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPanePokoje, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPodzielOpinia)
                    .addComponent(labelKomentarze))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonDodajKomentarz)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
			//main.table = (DefaultTableModel) tablePokoje.getModel();
			main.t.setModel(tablePokoje.getModel());
			for (int i = 0; i < main.atrakcje.length; ++i) {
				if (((DefaultListModel) jList1.getModel()).contains(main.atrakcje[i])) {
					main.boxesAtr[i].setSelected(true);
				} else {
					main.boxesAtr[i].setSelected(false);
				}
			}
			for (int i = 0; i < main.wyposazenie.length; ++i) {
				if (((DefaultListModel) jList2.getModel()).contains(main.wyposazenie[i])) {
					main.boxesWyp[i].setSelected(true);
				} else {
					main.boxesWyp[i].setSelected(false);
				}
			}
			main.przepisz(obiekt, labelNazwa.getText(), labelAdres.getText(), labelTelefon.getText(), labelWWW.getText(), lng.toString(), lat.toString());

			this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

	private void buttonDodajKomentarzActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDodajKomentarzActionPerformed
		try {
			ResultSet rs = SqlQueries.Insert(Tabele.komentarze, "obiekt, komentarz", obiekt + ", '" + textFieldKomentarz.getText()+"'", "data");
			rs.next();
                        textAreaKomentarze.append(rs.getString("data") + ": " + textFieldKomentarz.getText() + "\n\n");
			//((DefaultListModel)(listKomentarze.getModel())).addElement(rs.getString("data") + ": " +textFieldKomentarz.getText());
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage());
		}

	}//GEN-LAST:event_buttonDodajKomentarzActionPerformed
	/**
	 @param args the command line arguments
	 */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDodajKomentarz;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel labelAdres;
    private javax.swing.JLabel labelKomentarze;
    private javax.swing.JLabel labelNazwa;
    private javax.swing.JLabel labelPodzielOpinia;
    private javax.swing.JLabel labelTelefon;
    private javax.swing.JLabel labelTyp;
    private javax.swing.JLabel labelWWW;
    private javax.swing.JScrollPane scrollPanePokoje;
    private javax.swing.JTable tablePokoje;
    private javax.swing.JTextArea textAreaKomentarze;
    private javax.swing.JTextPane textFieldKomentarz;
    // End of variables declaration//GEN-END:variables
}
